<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>TemplateResponse and SimpleTemplateResponse &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../index.html" />
    <link rel="up" title="API Reference" href="index.html" />
    <link rel="next" title="Unicode data" href="unicode.html" />
    <link rel="prev" title="The Django template language: For Python programmers" href="templates/api.html" />



 
<script type="text/javascript" src="../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="templates/api.html" title="The Django template language: For Python programmers">previous</a>
     |
    <a href="index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="unicode.html" title="Unicode data">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-template-response">
            
  <div class="section" id="s-module-django.template.response">
<span id="s-templateresponse-and-simpletemplateresponse"></span><span id="module-django.template.response"></span><span id="templateresponse-and-simpletemplateresponse"></span><h1>TemplateResponse and SimpleTemplateResponse<a class="headerlink" href="#module-django.template.response" title="Permalink to this headline">¶</a></h1>
<p>Standard <a class="reference internal" href="request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><tt class="xref py py-class docutils literal"><span class="pre">HttpResponse</span></tt></a> objects are static structures.
They are provided with a block of pre-rendered content at time of
construction, and while that content can be modified, it isn&#8217;t in a form that
makes it easy to perform modifications.</p>
<p>However, it can sometimes be beneficial to allow decorators or
middleware to modify a response <em>after</em> it has been constructed by the
view. For example, you may want to change the template that is used,
or put additional data into the context.</p>
<p>TemplateResponse provides a way to do just that. Unlike basic
<a class="reference internal" href="request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><tt class="xref py py-class docutils literal"><span class="pre">HttpResponse</span></tt></a> objects, TemplateResponse objects retain
the details of the template and context that was provided by the view to
compute the response. The final output of the response is not computed until
it is needed, later in the response process.</p>
<div class="section" id="s-simpletemplateresponse-objects">
<span id="simpletemplateresponse-objects"></span><h2>SimpleTemplateResponse objects<a class="headerlink" href="#simpletemplateresponse-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.template.response.SimpleTemplateResponse">
<em class="property">class </em><tt class="descname">SimpleTemplateResponse</tt><a class="reference internal" href="../_modules/django/template/response.html#SimpleTemplateResponse"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.SimpleTemplateResponse" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="section" id="s-attributes">
<span id="attributes"></span><h3>Attributes<a class="headerlink" href="#attributes" title="Permalink to this headline">¶</a></h3>
<dl class="attribute">
<dt id="django.template.response.SimpleTemplateResponse.template_name">
<tt class="descclassname">SimpleTemplateResponse.</tt><tt class="descname">template_name</tt><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.template_name" title="Permalink to this definition">¶</a></dt>
<dd><p>The name of the template to be rendered. Accepts a
<a class="reference internal" href="templates/api.html#django.template.Template" title="django.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> object, a path to a template or list
of template paths.</p>
<p>Example: <tt class="docutils literal"><span class="pre">['foo.html',</span> <span class="pre">'path/to/bar.html']</span></tt></p>
</dd></dl>

<dl class="attribute">
<dt id="django.template.response.SimpleTemplateResponse.context_data">
<tt class="descclassname">SimpleTemplateResponse.</tt><tt class="descname">context_data</tt><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.context_data" title="Permalink to this definition">¶</a></dt>
<dd><p>The context data to be used when rendering the template. It can be
a dictionary or a context object.</p>
<p>Example: <tt class="docutils literal"><span class="pre">{'foo':</span> <span class="pre">123}</span></tt></p>
</dd></dl>

<dl class="attribute">
<dt id="django.template.response.SimpleTemplateResponse.rendered_content">
<tt class="descclassname">SimpleTemplateResponse.</tt><tt class="descname">rendered_content</tt><a class="reference internal" href="../_modules/django/template/response.html#SimpleTemplateResponse.rendered_content"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.rendered_content" title="Permalink to this definition">¶</a></dt>
<dd><p>The current rendered value of the response content, using the current
template and context data.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.template.response.SimpleTemplateResponse.is_rendered">
<tt class="descclassname">SimpleTemplateResponse.</tt><tt class="descname">is_rendered</tt><a class="reference internal" href="../_modules/django/template/response.html#SimpleTemplateResponse.is_rendered"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.is_rendered" title="Permalink to this definition">¶</a></dt>
<dd><p>A boolean indicating whether the response content has been rendered.</p>
</dd></dl>

</div>
<div class="section" id="s-methods">
<span id="methods"></span><h3>Methods<a class="headerlink" href="#methods" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.template.response.SimpleTemplateResponse.__init__">
<tt class="descclassname">SimpleTemplateResponse.</tt><tt class="descname">__init__</tt>(<em>template</em>, <em>context=None</em>, <em>content_type=None</em>, <em>status=None</em>)<a class="reference internal" href="../_modules/django/template/response.html#SimpleTemplateResponse.__init__"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Instantiates a
<a class="reference internal" href="#django.template.response.SimpleTemplateResponse" title="django.template.response.SimpleTemplateResponse"><tt class="xref py py-class docutils literal"><span class="pre">SimpleTemplateResponse</span></tt></a> object
with the given template, context, content type, and HTTP status.</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">template</span></tt></dt>
<dd>The full name of a template, or a sequence of template names.
<a class="reference internal" href="templates/api.html#django.template.Template" title="django.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> instances can also be used.</dd>
<dt><tt class="docutils literal"><span class="pre">context</span></tt></dt>
<dd>A dictionary of values to add to the template context. By default,
this is an empty dictionary. <a class="reference internal" href="templates/api.html#django.template.Context" title="django.template.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> objects
are also accepted as <tt class="docutils literal"><span class="pre">context</span></tt> values.</dd>
<dt><tt class="docutils literal"><span class="pre">status</span></tt></dt>
<dd>The HTTP Status code for the response.</dd>
<dt><tt class="docutils literal"><span class="pre">content_type</span></tt></dt>
<dd>The value included in the HTTP <tt class="docutils literal"><span class="pre">Content-Type</span></tt> header, including the
MIME type specification and the character set encoding. If
<tt class="docutils literal"><span class="pre">content_type</span></tt> is specified, then its value is used. Otherwise,
<a class="reference internal" href="settings.html#std:setting-DEFAULT_CONTENT_TYPE"><tt class="xref std std-setting docutils literal"><span class="pre">DEFAULT_CONTENT_TYPE</span></tt></a> is used.</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="django.template.response.SimpleTemplateResponse.resolve_context">
<tt class="descclassname">SimpleTemplateResponse.</tt><tt class="descname">resolve_context</tt>(<em>context</em>)<a class="reference internal" href="../_modules/django/template/response.html#SimpleTemplateResponse.resolve_context"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.resolve_context" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts context data into a context instance that can be used for
rendering a template. Accepts a dictionary of context data or a
context object. Returns a <a class="reference internal" href="templates/api.html#django.template.Context" title="django.template.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a>
instance containing the provided data.</p>
<p>Override this method in order to customize context instantiation.</p>
</dd></dl>

<dl class="method">
<dt id="django.template.response.SimpleTemplateResponse.resolve_template">
<tt class="descclassname">SimpleTemplateResponse.</tt><tt class="descname">resolve_template</tt>(<em>template</em>)<a class="reference internal" href="../_modules/django/template/response.html#SimpleTemplateResponse.resolve_template"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.resolve_template" title="Permalink to this definition">¶</a></dt>
<dd><p>Resolves the template instance to use for rendering. Accepts a
path of a template to use, or a sequence of template paths.
<a class="reference internal" href="templates/api.html#django.template.Template" title="django.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> instances may also be provided.
Returns the <a class="reference internal" href="templates/api.html#django.template.Template" title="django.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> instance to be
rendered.</p>
<p>Override this method in order to customize template rendering.</p>
</dd></dl>

<dl class="method">
<dt id="django.template.response.SimpleTemplateResponse.add_post_render_callback">
<tt class="descclassname">SimpleTemplateResponse.</tt><tt class="descname">add_post_render_callback</tt>()<a class="reference internal" href="../_modules/django/template/response.html#SimpleTemplateResponse.add_post_render_callback"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.add_post_render_callback" title="Permalink to this definition">¶</a></dt>
<dd><p>Add a callback that will be invoked after rendering has taken
place. This hook can be used to defer certain processing
operations (such as caching) until after rendering has occurred.</p>
<p>If the <a class="reference internal" href="#django.template.response.SimpleTemplateResponse" title="django.template.response.SimpleTemplateResponse"><tt class="xref py py-class docutils literal"><span class="pre">SimpleTemplateResponse</span></tt></a>
has already been rendered, the callback will be invoked
immediately.</p>
<p>When called, callbacks will be passed a single argument &#8211; the
rendered <a class="reference internal" href="#django.template.response.SimpleTemplateResponse" title="django.template.response.SimpleTemplateResponse"><tt class="xref py py-class docutils literal"><span class="pre">SimpleTemplateResponse</span></tt></a>
instance.</p>
<p>If the callback returns a value that is not <tt class="docutils literal"><span class="pre">None</span></tt>, this will be
used as the response instead of the original response object (and
will be passed to the next post rendering callback etc.)</p>
</dd></dl>

<dl class="method">
<dt id="django.template.response.SimpleTemplateResponse.render">
<tt class="descclassname">SimpleTemplateResponse.</tt><tt class="descname">render</tt>()<a class="reference internal" href="../_modules/django/template/response.html#SimpleTemplateResponse.render"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.SimpleTemplateResponse.render" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets <tt class="docutils literal"><span class="pre">response.content</span></tt> to the result obtained by
<a class="reference internal" href="#django.template.response.SimpleTemplateResponse.rendered_content" title="django.template.response.SimpleTemplateResponse.rendered_content"><tt class="xref py py-attr docutils literal"><span class="pre">SimpleTemplateResponse.rendered_content</span></tt></a>, runs all post-rendering
callbacks, and returns the resulting response object.</p>
<p><tt class="docutils literal"><span class="pre">render()</span></tt> will only have an effect the first time it is called. On
subsequent calls, it will return the result obtained from the first call.</p>
</dd></dl>

</div>
</div>
<div class="section" id="s-templateresponse-objects">
<span id="templateresponse-objects"></span><h2>TemplateResponse objects<a class="headerlink" href="#templateresponse-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="django.template.response.TemplateResponse">
<em class="property">class </em><tt class="descname">TemplateResponse</tt><a class="reference internal" href="../_modules/django/template/response.html#TemplateResponse"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.TemplateResponse" title="Permalink to this definition">¶</a></dt>
<dd><p><tt class="docutils literal"><span class="pre">TemplateResponse</span></tt> is a subclass of
<a class="reference internal" href="#django.template.response.SimpleTemplateResponse" title="django.template.response.SimpleTemplateResponse"><tt class="xref py py-class docutils literal"><span class="pre">SimpleTemplateResponse</span></tt></a> that uses
a <a class="reference internal" href="templates/api.html#django.template.RequestContext" title="django.template.RequestContext"><tt class="xref py py-class docutils literal"><span class="pre">RequestContext</span></tt></a> instead of
a <a class="reference internal" href="templates/api.html#django.template.Context" title="django.template.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a>.</p>
</dd></dl>

<div class="section" id="s-id1">
<span id="id1"></span><h3>Methods<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.template.response.TemplateResponse.__init__">
<tt class="descclassname">TemplateResponse.</tt><tt class="descname">__init__</tt>(<em>request</em>, <em>template</em>, <em>context=None</em>, <em>content_type=None</em>, <em>status=None</em>, <em>current_app=None</em>)<a class="reference internal" href="../_modules/django/template/response.html#TemplateResponse.__init__"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.template.response.TemplateResponse.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Instantiates an <tt class="docutils literal"><span class="pre">TemplateResponse</span></tt> object with the given
template, context, MIME type and HTTP status.</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">request</span></tt></dt>
<dd>An <a class="reference internal" href="request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><tt class="xref py py-class docutils literal"><span class="pre">HttpRequest</span></tt></a> instance.</dd>
<dt><tt class="docutils literal"><span class="pre">template</span></tt></dt>
<dd>The full name of a template, or a sequence of template names.
<a class="reference internal" href="templates/api.html#django.template.Template" title="django.template.Template"><tt class="xref py py-class docutils literal"><span class="pre">Template</span></tt></a> instances can also be used.</dd>
<dt><tt class="docutils literal"><span class="pre">context</span></tt></dt>
<dd>A dictionary of values to add to the template context. By default,
this is an empty dictionary. <a class="reference internal" href="templates/api.html#django.template.Context" title="django.template.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> objects
are also accepted as <tt class="docutils literal"><span class="pre">context</span></tt> values. If you pass a
<a class="reference internal" href="templates/api.html#django.template.Context" title="django.template.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> instance or subclass, it will be used
instead of creating a new <a class="reference internal" href="templates/api.html#django.template.RequestContext" title="django.template.RequestContext"><tt class="xref py py-class docutils literal"><span class="pre">RequestContext</span></tt></a>.</dd>
<dt><tt class="docutils literal"><span class="pre">status</span></tt></dt>
<dd>The HTTP Status code for the response.</dd>
<dt><tt class="docutils literal"><span class="pre">content_type</span></tt></dt>
<dd>The value included in the HTTP <tt class="docutils literal"><span class="pre">Content-Type</span></tt> header, including the
MIME type specification and the character set encoding. If
<tt class="docutils literal"><span class="pre">content_type</span></tt> is specified, then its value is used. Otherwise,
<a class="reference internal" href="settings.html#std:setting-DEFAULT_CONTENT_TYPE"><tt class="xref std std-setting docutils literal"><span class="pre">DEFAULT_CONTENT_TYPE</span></tt></a> is used.</dd>
<dt><tt class="docutils literal"><span class="pre">current_app</span></tt></dt>
<dd>A hint indicating which application contains the current view. See the
<a class="reference internal" href="../topics/http/urls.html#topics-http-reversing-url-namespaces"><em>namespaced URL resolution strategy</em></a>
for more information.</dd>
</dl>
</dd></dl>

</div>
</div>
<div class="section" id="s-the-rendering-process">
<span id="the-rendering-process"></span><h2>The rendering process<a class="headerlink" href="#the-rendering-process" title="Permalink to this headline">¶</a></h2>
<p>Before a <a class="reference internal" href="#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><tt class="xref py py-class docutils literal"><span class="pre">TemplateResponse</span></tt></a> instance can be
returned to the client, it must be rendered. The rendering process takes the
intermediate representation of template and context, and turns it into the
final byte stream that can be served to the client.</p>
<p>There are three circumstances under which a <tt class="docutils literal"><span class="pre">TemplateResponse</span></tt> will be
rendered:</p>
<ul class="simple">
<li>When the <tt class="docutils literal"><span class="pre">TemplateResponse</span></tt> instance is explicitly rendered, using
the <a class="reference internal" href="#django.template.response.SimpleTemplateResponse.render" title="django.template.response.SimpleTemplateResponse.render"><tt class="xref py py-meth docutils literal"><span class="pre">SimpleTemplateResponse.render()</span></tt></a> method.</li>
<li>When the content of the response is explicitly set by assigning
<tt class="docutils literal"><span class="pre">response.content</span></tt>.</li>
<li>After passing through template response middleware, but before
passing through response middleware.</li>
</ul>
<p>A <tt class="docutils literal"><span class="pre">TemplateResponse</span></tt> can only be rendered once. The first call to
<a class="reference internal" href="#django.template.response.SimpleTemplateResponse.render" title="django.template.response.SimpleTemplateResponse.render"><tt class="xref py py-meth docutils literal"><span class="pre">SimpleTemplateResponse.render()</span></tt></a> sets the content of the response;
subsequent rendering calls do not change the response content.</p>
<p>However, when <tt class="docutils literal"><span class="pre">response.content</span></tt> is explicitly assigned, the
change is always applied. If you want to force the content to be
re-rendered, you can re-evaluate the rendered content, and assign
the content of the response manually:</p>
<div class="highlight-python"><div class="highlight"><pre># Set up a rendered TemplateResponse
&gt;&gt;&gt; from django.template.response import TemplateResponse
&gt;&gt;&gt; t = TemplateResponse(request, &#39;original.html&#39;, {})
&gt;&gt;&gt; t.render()
&gt;&gt;&gt; print(t.content)
Original content

# Re-rendering doesn&#39;t change content
&gt;&gt;&gt; t.template_name = &#39;new.html&#39;
&gt;&gt;&gt; t.render()
&gt;&gt;&gt; print(t.content)
Original content

# Assigning content does change, no render() call required
&gt;&gt;&gt; t.content = t.rendered_content
&gt;&gt;&gt; print(t.content)
New content
</pre></div>
</div>
<div class="section" id="s-post-render-callbacks">
<span id="post-render-callbacks"></span><h3>Post-render callbacks<a class="headerlink" href="#post-render-callbacks" title="Permalink to this headline">¶</a></h3>
<p>Some operations &#8211; such as caching &#8211; cannot be performed on an
unrendered template. They must be performed on a fully complete and
rendered response.</p>
<p>If you&#8217;re using middleware, the solution is easy. Middleware provides
multiple opportunities to process a response on exit from a view. If
you put behavior in the Response middleware is guaranteed to execute
after template rendering has taken place.</p>
<p>However, if you&#8217;re using a decorator, the same opportunities do not
exist. Any behavior defined in a decorator is handled immediately.</p>
<p>To compensate for this (and any other analogous use cases),
<a class="reference internal" href="#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><tt class="xref py py-class docutils literal"><span class="pre">TemplateResponse</span></tt></a> allows you to register callbacks that will
be invoked when rendering has completed. Using this callback, you can
defer critical processing until a point where you can guarantee that
rendered content will be available.</p>
<p>To define a post-render callback, just define a function that takes
a single argument &#8211; response &#8211; and register that function with
the template response:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.template.response</span> <span class="kn">import</span> <span class="n">TemplateResponse</span>

<span class="k">def</span> <span class="nf">my_render_callback</span><span class="p">(</span><span class="n">response</span><span class="p">):</span>
    <span class="c"># Do content-sensitive processing</span>
    <span class="n">do_post_processing</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">my_view</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="c"># Create a response</span>
    <span class="n">response</span> <span class="o">=</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s">&#39;mytemplate.html&#39;</span><span class="p">,</span> <span class="p">{})</span>
    <span class="c"># Register the callback</span>
    <span class="n">response</span><span class="o">.</span><span class="n">add_post_render_callback</span><span class="p">(</span><span class="n">my_render_callback</span><span class="p">)</span>
    <span class="c"># Return the response</span>
    <span class="k">return</span> <span class="n">response</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">my_render_callback()</span></tt> will be invoked after the <tt class="docutils literal"><span class="pre">mytemplate.html</span></tt>
has been rendered, and will be provided the fully rendered
<a class="reference internal" href="#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><tt class="xref py py-class docutils literal"><span class="pre">TemplateResponse</span></tt></a> instance as an argument.</p>
<p>If the template has already been rendered, the callback will be
invoked immediately.</p>
</div>
</div>
<div class="section" id="s-using-templateresponse-and-simpletemplateresponse">
<span id="using-templateresponse-and-simpletemplateresponse"></span><h2>Using TemplateResponse and SimpleTemplateResponse<a class="headerlink" href="#using-templateresponse-and-simpletemplateresponse" title="Permalink to this headline">¶</a></h2>
<p>A TemplateResponse object can be used anywhere that a normal
HttpResponse can be used. It can also be used as an alternative to
calling <a class="reference internal" href="../topics/http/shortcuts.html#django.shortcuts.render_to_response" title="django.shortcuts.render_to_response"><tt class="xref py py-func docutils literal"><span class="pre">render_to_response()</span></tt></a>.</p>
<p>For example, the following simple view returns a
<a class="reference internal" href="#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><tt class="xref py py-class docutils literal"><span class="pre">TemplateResponse()</span></tt></a> with a simple template, and a context
containing a queryset:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.template.response</span> <span class="kn">import</span> <span class="n">TemplateResponse</span>

<span class="k">def</span> <span class="nf">blog_index</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">TemplateResponse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s">&#39;entry_list.html&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;entries&#39;</span><span class="p">:</span> <span class="n">Entry</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()})</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">TemplateResponse and SimpleTemplateResponse</a><ul>
<li><a class="reference internal" href="#simpletemplateresponse-objects">SimpleTemplateResponse objects</a><ul>
<li><a class="reference internal" href="#attributes">Attributes</a></li>
<li><a class="reference internal" href="#methods">Methods</a></li>
</ul>
</li>
<li><a class="reference internal" href="#templateresponse-objects">TemplateResponse objects</a><ul>
<li><a class="reference internal" href="#id1">Methods</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-rendering-process">The rendering process</a><ul>
<li><a class="reference internal" href="#post-render-callbacks">Post-render callbacks</a></li>
</ul>
</li>
<li><a class="reference internal" href="#using-templateresponse-and-simpletemplateresponse">Using TemplateResponse and SimpleTemplateResponse</a></li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="templates/api.html">The Django template language: For Python programmers</a></li>
    
    
      <li>Next: <a href="unicode.html">Unicode data</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="index.html">API Reference</a>
        
        <ul><li>TemplateResponse and SimpleTemplateResponse</li></ul>
        </li></ul>
      </li>
  </ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/ref/template-response.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="templates/api.html" title="The Django template language: For Python programmers">previous</a>
     |
    <a href="index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="unicode.html" title="Unicode data">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>